from sklearn.metrics import classification_report
import time
class showAndRecoedScores():
    def __init__(self):
        pass
    def getAllMethods(self):
        return (list(filter(lambda m: not m.startswith("__") \
                                      and not m.endswith("__") \
                                      and m.startswith("mx")
                                      and callable(getattr(self, m)),
                            dir(self))))
    def startAllAndShowResult(self):
        dicScore={}
        model_funcs=self.getAllMethods()
        for model_func in model_funcs:
            try:
                func = self.__getattribute__(model_func)
                knc = func(self.X_train, self.y_train)  # 训练数据集
                score = knc[0].score(self.X_test, self.y_test)  # 测试数据集
                print("----", score,knc[1])
                dicScore[knc[1]]=score

                time_start = time.time()  # time.time()为1970.1.1到当前时间的毫秒
                Y_pred = knc[0].predict(self.X_test)
                time_end = time.time()

                cost_time = str(time_end - time_start)
                ans = classification_report(self.y_test, Y_pred)
                dicScore[knc[1]] = (score, ans, cost_time)

            except:
                print("请查看数据集中是否有空值")
                print("Fail----",knc[1])

        dicScore = sorted(dicScore.items(),key=lambda x:x[1],reverse=True)  # 按字典集合中，每一个元组的第二个元素排列

        print("--------result---------")
        for item in dicScore:
            print(item[0] + ": " + str(item[1][0])+"      cost time:"+item[1][2])
            print(item[1][1])
        print("-----------------------")